'use strict';

var myApp = angular.module('myApp.controllers',[]);

/**
 * Controller that contains all medecins and the waiting list
 */
//myApp.controller('AllMedecinsCtrl', function ($scope, $http, $location, $routeParams,WaitingList) {
function AllMedecinsCtrl($scope, $http, $location, $routeParams,WaitingList,MedecinServices,PatientServices) {

	$scope.medecin = MedecinServices.getGlobalMedecin();
	
	$scope.getAllMedecins = function(dataString) {
		var convertToArray = dataString.replace(/ +/g,',');
		MedecinServices.getAll(convertToArray,function(data, status) {
				if (status != 200) {
					alert("Un probleme est survenu lors de la lecture des medecins");
					return;	
				}
				$scope.medecin_list = window.GLOBAL_MEDECIN_LIST = data;
				updateWorkingOn("medecin-all",PatientServices,MedecinServices);
			}
		)};
	
	$scope.createMedecin = function(nom,prenom,specialite) {
		$scope.medecin = {nom: nom, prenom: prenom, specialite: specialite};
		$scope.medecin.id = Math.floor((Math.random() * 10000));

		MedecinServices.saveData($scope.medecin,function(status) {
			if (status == 200) {
				$scope.medecin = MedecinServices.getGlobalMedecin();
				window.GLOBAL_PATIENT_LIST.push($scope.medecin);
				$scope.medecin = window.GLOBAL_PATIENT_LIST;
				viewMedecinDetails($scope.medecin);
			}
			else {
				alert("Erreur lors de la creation du medecin: "+nom+" "+prenom);
			}
		});

	};
	
	$scope.viewMedecinDetails = function(medecin) {
		MedecinServices.setGlobalMedecin({nom: medecin.nom, prenom : medecin.prenom, id: medecin.id, path: medecin.path});
		$location.url('/medecin-details');
	};
	
	$scope.removeMedecin = function(medecinJSON) {
		var index = window.GLOBAL_MEDECIN_LIST.indexOf(medecinJSON);
		if (index == -1) return;
		MedecinServices.deleteData(window.GLOBAL_MEDECIN_LIST[index],function(status){
			if(status==200){
				if (index >= 0) {
					window.GLOBAL_MEDECIN_LIST.splice(index,1);
					$scope.medecin_list = window.GLOBAL_MEDECIN_LIST;
				}
			}
			else alert("Probleme lors de la suppression du medecin");
		});
	};
	
	$scope.getTotalMedecins = function()
	{
		if (!window.GLOBAL_MEDECIN_LIST) return 0;
		return window.GLOBAL_MEDECIN_LIST.length;
	}

	$scope.getTotalMedecinsHere = function()
	{
		if (!window.GLOBAL_MEDECIN_LIST) return 0;
		return WaitingList.data.length;
	}
	
	$scope.getAllMedecins("");
	
}
//);

/**
 * Controller that contains medecin detailed information
 */

//myApp.controller('MedecinDetailCtrl', );
function MedecinDetailCtrl ($scope, $http, $location, $routeParams, MedecinServices, PatientServices) {

	$scope.saveMedecin = function(medecin) {
		MedecinServices.saveData(medecin,function(status){if(status!=200) alert("An error occurred");});
	}
	
	$scope.getMedecin = function(medecin) {
		if (!MedecinServices.getGlobalMedecin()) {
			updateWorkingOn("medecin-detail",PatientServices,MedecinServices);
			return;
		}
		MedecinServices.readData(MedecinServices.getGlobalMedecin(),function(data,status){
			(status==200)?
				$scope.medecin=data:
				alert("["+status+"] Une erreur s'est produite pendant la lecture du medecin: "+$scope.medecin.nom + " " +$scope.medecin.prenom);
			updateWorkingOn("medecin-detail",PatientServices,MedecinServices);
		});
	}
	
	$scope.getMedecin();

};